package de.kraenksoft.android.cinemaExample;

import java.util.List;
import java.util.Map;

import android.app.ListActivity;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import de.kraenksoft.android.cinemaExample.presenters.MovieListPresenter;
import de.kraenksoft.android.cinemaExample.views.MovieListView;

public class UFProgramActivity extends ListActivity implements MovieListView{

	private TextView viewCaption = null;
	private MovieListPresenter presenter;
	private ProgressDialog pd;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		// We use a custom layout here. It must define a
		// list view of id "list"
		setContentView(R.layout.main);
		
		viewCaption = (TextView) findViewById(R.id.date_caption);
		
		presenter = ((UFProgramApplication) getApplication()).getMovieListPresenter();
		presenter.setView(this);
	}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.main_menu, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		((UFProgramApplication) getApplication()).refreshTheater();
		return true;
	}

	public void nextDay(View v) {
		presenter.nextDayClicked();
	}

	public void lastDay(View v) {
		presenter.lastDayClicked();
	}

	@Override
	public void setMovies(List<Map<String, String>> movies) {
		final String[] from = { MOVIENAMEKEY, MOVIESCREENINGSKEY };
		final int[] to = { R.id.movieName, R.id.movieScreenings };

		SimpleAdapter adapter = new SimpleAdapter(getApplicationContext(),
				movies, R.layout.table_list_item, from, to);
		setListAdapter(adapter);
	}

	@Override
	public void setCaption(String caption) {
		viewCaption.setText(caption);
	}

	@Override
	public void setNextDayButtonVisible(boolean visible) {
		if(visible){
			findViewById(R.id.ibtnNext).setVisibility(View.VISIBLE);
		}else{
			findViewById(R.id.ibtnNext).setVisibility(View.INVISIBLE);
		}
	}

	@Override
	public void setLastDayButtonVisible(boolean visible) {
		if(visible){
			findViewById(R.id.ibtnLast).setVisibility(View.VISIBLE);
		}else{
			findViewById(R.id.ibtnLast).setVisibility(View.INVISIBLE);
		}
	}
	
	@Override
	public void showLoadingData(boolean active){
		String loadingData = getApplicationContext().getResources().getString(R.string.loading_data);
		String accesingData = getApplicationContext().getResources().getString(R.string.accesing_server);
		if(active){
			pd = ProgressDialog.show(this, loadingData, accesingData, true, false);
		}else{
			if (pd != null) pd.dismiss();
		}
	}

	@Override
	public void setErrorText(String errorText) {
		((TextView) findViewById(android.R.id.empty)).setText(errorText);
	}

}